# coding:utf-8
from selenium.webdriver.remote.webdriver import WebDriver

__author__ = 'Helen'
'''
description:UI页面公共类
'''
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException


class BasePage:
    def __init__(self, driver: WebDriver):
        self.driver = driver

    def find_element(self, *loc):
        '''重写find_element方法，显式等待'''
        try:
            WebDriverWait(self.driver, 3).until(EC.visibility_of_element_located(loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            raise e

    def send_keys(self, value, *loc):
        try:
            self.find_element(*loc).clear()
            self.find_element(*loc).send_keys(value)
        except AttributeError as e:
            raise e

    def chech_login2(self):
        print("检查是否已经有登陆账号")
        try:
            el6 = self.driver.find_element_by_id("com.xqxc.customer:id/settingIbtn")

        except NoSuchElementException:
            print("没有账号登陆")
            back = self.driver.find_element_by_id("com.xqxc.customer:id/mBackIV")
            back.click()
        else:
            el6.click()
            el7 = self.driver.find_element_by_xpath(
                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.Button")
            el7.click()
            el8 = self.driver.find_element_by_id("com.xqxc.customer:id/account_pwd_tip")
            el8.click()
            back = self.driver.find_element_by_id("com.xqxc.customer:id/mBackIV")
            back.click()
            return self


